package com.service;

import com.mapper.ItemMapper;
import com.mapper.OptionMapper;
import com.mapper.SubjectMapper;
import com.mapper.UserMapper;
import com.entity.Option;
import com.entity.Subject;
import com.entity.UserInfo;
import com.entity.vo.SubjectVo;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

@Service
public class SubjectService {
    @Resource
    private SubjectMapper subjectMapper;
    @Resource
    private OptionMapper optionMapper;
    @Resource
    private ItemMapper itemMapper;
    //首页分页查询
    public List<SubjectVo> selectSubjectDetailInfo(Integer index,Integer size){
        System.out.println("service   >>>>>>>>>>>>>>> ");
        List<SubjectVo> lists =  this.subjectMapper.selectSubjectDetail(index, size);
       return  lists;
    }
    public PageInfo<SubjectVo> selectSubjectDetailAdmin(Integer index,Integer size) {
    	PageHelper.startPage(index, size);//PageHelper 来自分页插件
    	List<SubjectVo> lists =  this.subjectMapper.selectSubjectDetailAdmin(index, size);
		PageInfo<SubjectVo> info = new PageInfo<SubjectVo>(lists); //PageInfo 来自分页插件
		//int[] nums = info.getNavigatepageNums();
    	 return  info;
    }
    public Integer selectSubjectCount() {
    	return this.subjectMapper.selectSubjectDeailCount();
    }
    // 根据vsid 查询subject 详细
    public SubjectVo selectOneSubjectDetail(Integer subid) {
    	System.out.println("subid = " + subid);
    	return this.subjectMapper.selectOneSubjectDeail(subid);
    }
    //根据vsid查询投票 结果
    public List<Map> selectSubjectOptionBl(Integer vsid){
    	return this.subjectMapper.selectOptionBL(vsid);
    }
    //������ͶƱ  sub:����  opinfo: ѡ������
    //return 1 �����ɹ� return 2 ���������ظ�
    public Integer saveSubject( Subject sub, String[] opinfo) {
    	System.out.println(sub.getVs_title() + " >>>>>>>>>>>>>>> ");
    	System.out.println(sub.getVs_type() + " >>>>>>>>>>>>>> ");
    	Subject subinfo =  this.subjectMapper.selectSubjectByName(sub.getVs_title().trim());
    	if(subinfo == null) {//˵�����ⲻ���� ���Է��� 
    	   	Integer temp =  this.subjectMapper.saveSubject(sub); //����subject
        	if(temp > 0 ) {//����subject�ɹ� 
        		Integer vsid =  this.subjectMapper.selectMaxId();
        		for(int i = 0 ; i < opinfo.length;i++) {
        			Option o = new Option();
        			o.setVo_option(opinfo[i]);
        			o.setVs_id(vsid);
        			Integer t = this.optionMapper.saveOption(o);
        			if( t == 0) {
        				throw new RuntimeException("���� ���� ѡ�� ������ ");
        			}
        		}
        	}
        	return 1;
    	}else {
    		return 2; //���������ظ�
    	}
    }
	//删除subject return 2不能删除 ， return 1 可以删除
	@Transactional
    public Integer deleteSubjectByid(Integer vsid) {
    	Integer temp =  this.itemMapper.selectItemByVsid(vsid);
    	if(temp == 0) {//该标题未被投过票 可以删除
    		////先删除subject对应的选项  option
    		List<Option> ops =  this.optionMapper.selectOptionByVsId(vsid);
    		for(Option o : ops) {
    			Integer os = this.optionMapper.deleteOptionById(o.getVo_id());
    			//System.out.println("os = " + os);
    		}
			//再删除对应的subject
    		 this.subjectMapper.deleteSubjectById(vsid);
    		return 1;
    	}else {
    		return 2; //该标题不能删除,因为已经投过票了
    	}
    }
	//查询一个subject
    public Subject selectOneSubject(Integer vsid) {
    	return this.subjectMapper.selectSubjectById(vsid);
    }
}
